package com.neusoft.service.impl;

import com.neusoft.entity.Disease;
import com.neusoft.entity.MedicalRecord;
import com.neusoft.mapper.DiseaseMapper;
import com.neusoft.mapper.MedicalRecordDiseaseMapper;
import com.neusoft.mapper.MedicalRecordMapper;
import com.neusoft.service.MedicalRecordService;
import com.neusoft.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author akemihomurasama
 */
@Service
public class MedicalRecordServiceImpl implements MedicalRecordService {
    @Autowired
    private MedicalRecordMapper medicalRecordMapper;
    @Autowired
    private MedicalRecordDiseaseMapper medicalRecordDiseaseMapper;
    @Autowired
    private DiseaseMapper diseaseMapper;

    /**
     * 存储患者病历首页信息
     *
     * @param medicalRecord 患者病历首页信息
     */
    @Override
    public Result saveRecord(MedicalRecord medicalRecord) {
        medicalRecordMapper.saveRecord(medicalRecord);
        return Result.ok();
    }

    /**
     * 根据患者病历id查询病历首页信息
     *
     * @param caseNumber 病历id
     * @return 病历首页信息
     */
    @Override
    public Result queryRecordByCaseNum(String caseNumber) {
        return Result.ok(medicalRecordMapper.queryRecordByCaseNum(caseNumber));
    }

    /**
     * 更新病历首页信息
     *
     * @param medicalRecord 信息
     * @return 更新结果
     */
    @Override
    public Result updateRecord(MedicalRecord medicalRecord) {
        medicalRecordMapper.updateRecord(medicalRecord);
        return Result.ok();
    }

    /**
     * 根据患病历首页id查询病历中的疾病列表
     *
     * @param recordId 病历首页id
     * @return 疾病列表
     */
    @Override
    public Result queryDiseaseListByRecordId(Integer recordId) {
        //1、根据病历首页id查询关联表得到疾病id列表
        List<Integer> diseaseIdList = medicalRecordDiseaseMapper.queryDiseaseIdListByRecordId(recordId);
        //如果疾病id列表为空，说明当前患者暂未选择病历
        if (diseaseIdList.size() == 0) {
            return Result.fail("患者无病历");
        }
        //2、根据疾病id列表查询疾病表获得疾病信息列表
        List<Disease> diseaseList = diseaseMapper.queryDiseaseByIdList(diseaseIdList);
        return Result.ok(diseaseList);
    }


}
